package com.androider.timememory.album;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import com.androider.timememory.R;
import com.androider.timememory.activity.AlbumPlayAudio;
import com.androider.timememory.activity.AlbumPlayPhoto;
import com.androider.timememory.activity.AlbumPlayVideo;
import com.androider.timememory.activity.Video;
import com.androider.timememory.utils.FileUtil;
import com.androider.timememory.utils.ImageUtil;
import com.androider.timememory.utils.RecordUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;

public class AlbumPageFactory implements IPageFactory {
	
	private final String TAG = "AlbumPageFactory";
	private ArrayList<HashMap<String, Object>> mData = null;
	private ArrayList<AlbumRecord> mRealDataList = null;
//	private ArrayList<AlbumRecord> mPageDataList = null;
	private ArrayList<Integer> mTopRecordIndexList = null;
	private int mPageAllNum = 0; /* The number of page */
	private int mPageIndex = 0; /* The index of page */
	private int mRecordIndex = 0; /* The index of record for all records */
	private int mPageRecordIndex = 0; /*
									 * The index of record for records in each
									 * page
									 */

	private int mAvailableCell = 0; /* The available cell could be used */
	private int mCurrentCell = 0; /* The current cell could be used */

	private int DEVICE_WIDTH;
	private int DEVICE_HEIGHT;
	private int PAGE_TOP_BOTTOM = 40;
	private int LINE_HEIGHT = 35;
	private int CELL_HEIGHT = 0;
	private int CELL_TOP = 40;
	private int CELL_PADDING = 20;
	private final int CELL_NUM = 6;

	private int mTopIndexInPage = 0;
	private int mTopIndexInNextPage = 0;
	
	private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	private Paint mDatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	private Bitmap bgBitmap;
	private Bitmap mTextBar = null;

	private AlbumRecord mRecord = null;
	private Context mContext;

	static private RecordType[] recordTypes = null;
	private RecordType currentRType;
	
	class AlbumRecord {
		Bitmap image;
		String note;
		String type;
		String date;
		String content;
		Rect rect;
		int typeIndex;
	}

	public AlbumPageFactory(int w, int h, Context context) {
		mContext = context;
		DEVICE_WIDTH = w;
		DEVICE_HEIGHT = h;
		CELL_HEIGHT = (DEVICE_HEIGHT - PAGE_TOP_BOTTOM * 2) / CELL_NUM;
		mRealDataList = new ArrayList<AlbumRecord>();
//		mPageDataList = new ArrayList<AlbumRecord>();
		mTopRecordIndexList = new ArrayList<Integer>();
		
		mPaint.setTextAlign(Align.LEFT);
		mPaint.setTextSize(24);
		mPaint.setColor(Color.BLACK);
		
		mDatePaint.setTextAlign(Align.LEFT);
		mDatePaint.setTextSize(24);
		mDatePaint.setColor(0xffec711c);
		
		
		if(recordTypes == null){
			recordTypes = new RecordType[]{
					new RecordType(BitmapFactory.decodeResource(mContext.getResources(),
									R.drawable.album_photo_frame_02),
									CELL_TOP, CELL_PADDING, 260, 160,
									CELL_TOP, 280 + CELL_PADDING, DEVICE_WIDTH - CELL_PADDING, 0),
					new RecordType(BitmapFactory.decodeResource(mContext.getResources(),
									R.drawable.album_photo_frame_02),
									CELL_TOP, DEVICE_WIDTH - CELL_PADDING - 260, 260, 160,
									CELL_TOP, CELL_PADDING, DEVICE_WIDTH - CELL_PADDING - 260, 0),
					
									
					new RecordType(BitmapFactory.decodeResource(mContext.getResources(),
									R.drawable.album_photo_frame_01),
									CELL_TOP, CELL_PADDING, 260, 160,
									CELL_TOP, 280 + CELL_PADDING, DEVICE_WIDTH - CELL_PADDING, 0),
					new RecordType(BitmapFactory.decodeResource(mContext.getResources(),
									R.drawable.album_photo_frame_01),
									CELL_TOP, DEVICE_WIDTH - CELL_PADDING - 260, 260, 160,
									CELL_TOP, CELL_PADDING, DEVICE_WIDTH - CELL_PADDING - 260, 0),
									
					/* 4,5 */		
					new RecordType(BitmapFactory.decodeResource(mContext.getResources(),
									R.drawable.album_movie_frame),
									CELL_TOP, CELL_PADDING + 5, 250, 150,
									CELL_TOP, 265 + CELL_PADDING, DEVICE_WIDTH - CELL_PADDING, 0),
					new RecordType(BitmapFactory.decodeResource(mContext.getResources(),
									R.drawable.album_movie_frame),
									CELL_TOP, DEVICE_WIDTH - CELL_PADDING - 255, 250, 150,
									CELL_TOP, CELL_PADDING, DEVICE_WIDTH - CELL_PADDING - 265, 0),
					/* 6 */
					new RecordType(BitmapFactory.decodeResource(mContext.getResources(),
									R.drawable.album_audio_frame),
									CELL_TOP/2, DEVICE_WIDTH - CELL_PADDING - 210, 207, 150,
									CELL_TOP, CELL_PADDING, DEVICE_WIDTH - CELL_PADDING - 210, 0),
				
			};
		}
	}

	@Override
	public void setData(ArrayList<HashMap<String, Object>> data) {
		mData = data;
		
		int neededCell = 0;
		this.mAvailableCell = CELL_NUM;
		for (int i = 0; i < mData.size(); i++) {
			mRecord = new AlbumRecord();
			mRecord.type = (String) mData.get(i).get("type");
			mRecord.note = (String) mData.get(i).get("note");
			if (mRecord.type.equals(RecordUtil.LIST_TYPE_PHOTO)){
				mRecord.image = ImageUtil.BitmapZoomClip(ImageUtil.getThumbnail(mContext, (String) mData
						.get(i).get("content"), mRecord.type), 260, 160);
				mRecord.typeIndex = mRecord.note.length() % 2;
			}else if (mRecord.type.equals(RecordUtil.LIST_TYPE_VIDEO)){
				mRecord.image = ImageUtil.BitmapZoomClip(ImageUtil.getThumbnail(mContext, (String) mData
						.get(i).get("content"), mRecord.type), 250, 150);
				mRecord.typeIndex = 4 + mRecord.note.length() % 2;
			}else if (mRecord.type.equals(RecordUtil.LIST_TYPE_AUDIO)){
				mRecord.typeIndex = 6;
			}
			mRecord.rect = new Rect();
			mRecord.date = (String) mData.get(i).get("date");
			mRecord.content = (String) mData.get(i).get("content");
			mRealDataList.add(mRecord);

			if (this.mAvailableCell == CELL_NUM) {
				mTopRecordIndexList.add(i);
				mPageAllNum++;
			}

			neededCell = 2; /* each record consume 2 cells at least */
			if (mRecord.type.equals(RecordUtil.LIST_TYPE_TEXT) && (mRecord.note.length() > 60)) {
				neededCell += ((mRecord.note.length() - 60) / 60);
			} else if (mRecord.type.equals(RecordUtil.LIST_TYPE_AUDIO) && (mRecord.note.length() > 60)) {
				neededCell += ((mRecord.note.length() - 60) / 30);
			} else if (mRecord.type.equals(RecordUtil.LIST_TYPE_PHOTO)
					|| mRecord.type.equals(RecordUtil.LIST_TYPE_VIDEO)) {
				neededCell += (mRecord.note.length() - 15) / 30;
			}
			if (this.mAvailableCell - neededCell < 0) { /* the rest cell is not enough, roll back and next page */
				i--;
				mRealDataList.remove(mRecord);
				this.mAvailableCell = CELL_NUM;
			} else if (this.mAvailableCell - neededCell < 2) { /* the rest cell is not enough, next page */
				this.mAvailableCell = CELL_NUM;
			} else {
				this.mAvailableCell -= neededCell;
			}

		}

//		setRecordIndex();
//		for(int i = mTopIndexInPage;i < mTopIndexInNextPage;i++){
//			mRecord = mRealDataList.get(i);
//			if (mRecord.type.equals(RecordUtils.LIST_TYPE_PHOTO)
//					|| mRecord.type.equals(RecordUtils.LIST_TYPE_VIDEO)){
//				mRecord.image = ImageUtils.BitmapZoom(ImageUtils.getThumbnail(mContext, (String) mData
//						.get(i).get("content"), mRecord.type), 300, 150);
//			}
//		}
		
		mTextBar = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.album_text_bar);
		
	}

	@Override
	public void openFile(String strFilePath) throws IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public void prePage() {
		if (mPageIndex > 0) {
//			setRecordIndex();
//			for(int i = mTopIndexInPage;i < mTopIndexInNextPage;i++){
//				mRecord = mRealDataList.get(i);
//				if ((mRecord.type.equals(RecordUtils.LIST_TYPE_PHOTO)
//						|| mRecord.type.equals(RecordUtils.LIST_TYPE_VIDEO)
//						&& mRecord.image != null)){
//					mRecord.image.recycle();
//				}
//			}
			mPageIndex--;
//			setRecordIndex();
//			for(int i = mTopIndexInPage;i < mTopIndexInNextPage;i++){
//				mRecord = mRealDataList.get(i);
//				if (mRecord.type.equals(RecordUtils.LIST_TYPE_PHOTO)
//						|| mRecord.type.equals(RecordUtils.LIST_TYPE_VIDEO)){
//					mRecord.image = ImageUtils.BitmapZoom(ImageUtils.getThumbnail(mContext, (String) mData
//							.get(i).get("content"), mRecord.type), 300, 150);
//				}
//			}
		}
	}

	@Override
	public void nextPage() {
		if (mPageIndex < mPageAllNum - 1) {
//			setRecordIndex();
//			for(int i = mTopIndexInPage;i < mTopIndexInNextPage;i++){
//				mRecord = mRealDataList.get(i);
//				if ((mRecord.type.equals(RecordUtils.LIST_TYPE_PHOTO)
//						|| mRecord.type.equals(RecordUtils.LIST_TYPE_VIDEO)
//						&& mRecord.image != null)){
//					mRecord.image.recycle();
//				}
//			}
			mPageIndex++;
//			setRecordIndex();
//			for(int i = mTopIndexInPage;i < mTopIndexInNextPage;i++){
//				mRecord = mRealDataList.get(i);
//				if (mRecord.type.equals(RecordUtils.LIST_TYPE_PHOTO)
//						|| mRecord.type.equals(RecordUtils.LIST_TYPE_VIDEO)){
//					mRecord.image = ImageUtils.BitmapZoom(ImageUtils.getThumbnail(mContext, (String) mData
//							.get(i).get("content"), mRecord.type), 300, 150);
//				}
//			}
		}
	}

	int usedCell = 0;
	@Override
	public void onDraw(Canvas c) {

		c.drawBitmap(bgBitmap, 0, 0, null);
		
		setRecordIndex();
		
		mCurrentCell = 0;
		
		for(int i = mTopIndexInPage;i < mTopIndexInNextPage;i++){
			mRecord = mRealDataList.get(i);
			currentRType = recordTypes[mRecord.typeIndex];
			usedCell = 2;
			/* Draw the date */
			c.drawCircle(CELL_PADDING - 2, mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM + 26, 3, mDatePaint);
			drawText(c, mDatePaint, " " + mRecord.date,
					CELL_PADDING,
					mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM,
					DEVICE_WIDTH - CELL_PADDING,
					(mCurrentCell + usedCell) * CELL_HEIGHT - CELL_PADDING,
					LINE_HEIGHT);
			
			if(mRecord.type.equals(RecordUtil.LIST_TYPE_TEXT)){
				/* Draw the note record */
				if(mRecord.note.length() > 60){
					usedCell += ((mRecord.note.length() - 60) / 60);
				}

				c.drawBitmap(mTextBar, CELL_PADDING, mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM + CELL_TOP
						, null);
				drawText(c, mPaint, "         " + mRecord.note,
						CELL_PADDING,
						mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM + CELL_TOP,
						DEVICE_WIDTH - CELL_PADDING,
						(mCurrentCell + usedCell) * CELL_HEIGHT - CELL_PADDING,
						LINE_HEIGHT);
			}else if(mRecord.type.equals(RecordUtil.LIST_TYPE_AUDIO)){
				/* Draw the audio record */
				drawText(c, mPaint, mRecord.note,
						currentRType.txtLeft,
						currentRType.txtTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM,
						currentRType.txtRight,
						(mCurrentCell + usedCell) * CELL_HEIGHT - CELL_PADDING,
						LINE_HEIGHT);
				
				c.drawBitmap(currentRType.frameBitmap, currentRType.imgLeft,
						currentRType.imgTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM, null);
				mRecord.image = currentRType.frameBitmap;
				setRectToRecord(mRecord,currentRType.imgLeft,
						currentRType.imgTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM);
			}else if(mRecord.type.equals(RecordUtil.LIST_TYPE_PHOTO)){
				/* Draw the photo record */
				drawText(c, mPaint, mRecord.note,
						currentRType.txtLeft,
						currentRType.txtTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM,
						currentRType.txtRight,
						(mCurrentCell + usedCell) * CELL_HEIGHT - CELL_PADDING,
						LINE_HEIGHT);
				
				c.drawBitmap(mRecord.image, currentRType.imgLeft + 5,
						currentRType.imgTop + 5 + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM, null);
				c.drawBitmap(currentRType.frameBitmap, currentRType.imgLeft,
						currentRType.imgTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM, null);
				
				setRectToRecord(mRecord,currentRType.imgLeft,
						currentRType.imgTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM);
			}else if(mRecord.type.equals(RecordUtil.LIST_TYPE_VIDEO)){
				/* Draw the video record */
				drawText(c, mPaint, mRecord.note,
						currentRType.txtLeft,
						currentRType.txtTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM,
						currentRType.txtRight,
						(mCurrentCell + usedCell) * CELL_HEIGHT - CELL_PADDING,
						LINE_HEIGHT);
				
				c.drawBitmap(mRecord.image, currentRType.imgLeft,
						currentRType.imgTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM, null);
				c.drawBitmap(currentRType.frameBitmap, currentRType.imgLeft,
						currentRType.imgTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM, null);
				
				setRectToRecord(mRecord,currentRType.imgLeft,
						currentRType.imgTop + mCurrentCell * CELL_HEIGHT + PAGE_TOP_BOTTOM);
			}
			mCurrentCell += usedCell;
		}

	}

	private void setRecordIndex(){
		mTopIndexInPage = this.mTopRecordIndexList.get(this.mPageIndex);
		if(mPageIndex < mPageAllNum - 1){
			mTopIndexInNextPage = this.mTopRecordIndexList.get(this.mPageIndex + 1);;
		}else{
			mTopIndexInNextPage = this.mData.size();
		}
	}
	
	private void setRectToRecord(AlbumRecord record,int left, int top){
		record.rect.set(left, top, left + record.image.getWidth(), top + record.image.getHeight());
	}
	
	private void drawText(Canvas c, Paint paint, String str, int left, int top,
			int right, int bottom, int linespacing) {
		String bb;
		for (int i = 1, a = 0;; i++) {
			// if((top + (i + 1) * linespacing) > bottom){
			// c.drawText("...", left, top + i * linespacing, paint);
			// break;
			// }
			//
			a = paint.breakText(str, true, right - left, null);
			if (a <= 0)
				break;
			bb = str.substring(0, a);
			str = str.substring(a);
			c.drawText(bb, left, top + i * linespacing, paint);
		}
	}

	@Override
	public void onClick(float x, float y) {

		for(int i = mTopIndexInPage;i < mTopIndexInNextPage;i++){
			mRecord = mRealDataList.get(i);
			if (x > mRecord.rect.left && x < mRecord.rect.right && y > mRecord.rect.top && y < mRecord.rect.bottom) {
				if (mRecord.type.equals(RecordUtil.LIST_TYPE_AUDIO)) {
					Intent intent = new Intent(mContext, AlbumPlayAudio.class);
					intent.putExtra(AlbumPlayAudio.AUDIO_PATH, mRecord.content);
					intent.putExtra(AlbumPlayAudio.AUDIO_TITLE, mRecord.note);
					mContext.startActivity(intent);
				} else if (mRecord.type.equals(RecordUtil.LIST_TYPE_PHOTO)) {
					Intent intent = new Intent(mContext, AlbumPlayPhoto.class);
					intent.putExtra(AlbumPlayPhoto.PHOTO_PATH, mRecord.content);
					mContext.startActivity(intent);
					((Activity) mContext).overridePendingTransition(
					R.anim.activity_zoomin, R.anim.activity_zoomout);
				}else if (mRecord.type.equals(RecordUtil.LIST_TYPE_VIDEO)) {
					Intent intent = new Intent(mContext, AlbumPlayVideo.class);
					intent.putExtra(AlbumPlayVideo.VIDEO_PATH, mRecord.content);
					mContext.startActivity(intent);
				}
			}
		}
		
	}

	@Override
	public void setBgBitmap(Bitmap BG) {
		bgBitmap = BG;
	}

	@Override
	public boolean isFirstPage() {
		return (mPageIndex == 0);
	}

	@Override
	public boolean isLastPage() {
		return (mPageAllNum == 0) || (mPageIndex == mPageAllNum - 1);
	}

	@Override
	public String getCurrentPic() {
		return null;
	}


	

}
